<!-- This comment will put IE 6, 7 and 8 in quirks mode -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>examples/Benchmark/BLAS/conv2d.cpp Source File</title>
<script type="text/javaScript" src="search/search.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3.0.1/es5/tex-mml-chtml.js"></script>
<script src="../../mlstyle.js"></script>
<link href="../css/besser.css" rel="stylesheet" type="text/css"/>
</head>
<!-- pretty cool: each body gets an id tag which is the basename of the web page  -->
<!--              and allows for page-specific CSS. this is client-side scripted, -->
<!--              so the id will not yet show up in the served source code -->
<script type="text/javascript">
    jQuery(document).ready(function () {
        var url = jQuery(location).attr('href');
        var pname = url.substr(url.lastIndexOf("/")+1, url.lastIndexOf(".")-url.lastIndexOf("/")-1);
        jQuery('#this_url').html('<strong>' + pname + '</strong>');
        jQuery('body').attr('id', pname);
    });
</script>
<body>
    <div id="shark_old">
        <div id="wrap">
            <div id="header">
                <div id="site-name"><a href="../../sphinx_pages/build/html/index.html">Shark machine learning library</a></div>
                <ul id="nav">
                    <li >
                        <a href="../../sphinx_pages/build/html/rest_sources/installation.html">Installation</a>
                    </li>
		    <li >
                        <a href="../../sphinx_pages/build/html/rest_sources/tutorials/tutorials.html">Tutorials</a>
                    </li>
		    <li >
                        <a href="../../sphinx_pages/build/html/rest_sources/benchmark.html">Benchmarks</a>
                    </li>
                    <li class="active">
                        <a href="classes.html">Documentation</a>
                        <ul>
                            <li class="first"></li>
                            <li><a href="../../sphinx_pages/build/html/rest_sources/quickref/quickref.html">Quick references</a></li>
                            <li><a href="classes.html">Class list</a></li>
                            <li class="last"><a href="group__shark__globals.html">Global functions</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
    </div>
<div id="doxywrapper">
<!--
    <div id="global_doxytitle">Doxygen<br>Documentation:</div>
-->
    <div id="navrow_wrapper">
<!-- Generated by Doxygen 1.9.8 -->
<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_d28a4824dc47e487b107a5db32ef43c4.html">examples</a></li><li class="navelem"><a class="el" href="dir_57865dd379b9744e2314c42289a7a619.html">Benchmark</a></li><li class="navelem"><a class="el" href="dir_d0a88e65ebc1a2a6b64ca06baf361bf8.html">BLAS</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle"><div class="title">conv2d.cpp</div></div>
</div><!--header-->
<div class="contents">
<a href="conv2d_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a id="l00001" name="l00001"></a><span class="lineno"><a class="line" href="conv2d_8cpp.html#ac7c698899b1a1a0db72e1b10c07abfb3">    1</a></span><span class="preprocessor">#define SHARK_USE_SIMD</span></div>
<div class="line"><a id="l00002" name="l00002"></a><span class="lineno">    2</span><span class="preprocessor">#include &lt;shark/LinAlg/BLAS/blas.h&gt;</span></div>
<div class="line"><a id="l00003" name="l00003"></a><span class="lineno">    3</span><span class="preprocessor">#include &lt;<a class="code" href="conv2d_8hpp.html">shark/LinAlg/BLAS/kernels/conv2d.hpp</a>&gt;</span></div>
<div class="line"><a id="l00004" name="l00004"></a><span class="lineno">    4</span><span class="preprocessor">#include &lt;<a class="code" href="_timer_8h.html">shark/Core/Timer.h</a>&gt;</span></div>
<div class="line"><a id="l00005" name="l00005"></a><span class="lineno">    5</span><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"><a id="l00006" name="l00006"></a><span class="lineno">    6</span><span class="keyword">using namespace </span><a class="code hl_namespace" href="namespaceshark.html" title="AbstractMultiObjectiveOptimizer.">shark</a>;</div>
<div class="line"><a id="l00007" name="l00007"></a><span class="lineno">    7</span><span class="keyword">using namespace </span>std;</div>
<div class="line"><a id="l00008" name="l00008"></a><span class="lineno">    8</span> </div>
<div class="line"><a id="l00009" name="l00009"></a><span class="lineno">    9</span><span class="keyword">template</span>&lt;<span class="keyword">class</span> E1, <span class="keyword">class</span> E2&gt;</div>
<div class="foldopen" id="foldopen00010" data-start="{" data-end="}">
<div class="line"><a id="l00010" name="l00010"></a><span class="lineno"><a class="line" href="conv2d_8cpp.html#aa29c4b1ecd308e3486ba80c16f257484">   10</a></span><span class="keywordtype">void</span> <a class="code hl_function" href="conv2d_8cpp.html#aa29c4b1ecd308e3486ba80c16f257484">benchmark</a>(</div>
<div class="line"><a id="l00011" name="l00011"></a><span class="lineno">   11</span>    blas::matrix_expression&lt;E1, blas::cpu_tag&gt; <span class="keyword">const</span>&amp; image,</div>
<div class="line"><a id="l00012" name="l00012"></a><span class="lineno">   12</span>    blas::matrix_expression&lt;E2, blas::cpu_tag&gt; <span class="keyword">const</span>&amp; filter,</div>
<div class="line"><a id="l00013" name="l00013"></a><span class="lineno">   13</span>    std::size_t num_channels,</div>
<div class="line"><a id="l00014" name="l00014"></a><span class="lineno">   14</span>    std::size_t num_filters</div>
<div class="line"><a id="l00015" name="l00015"></a><span class="lineno">   15</span>){</div>
<div class="line"><a id="l00016" name="l00016"></a><span class="lineno">   16</span>    std::size_t filter_size = filter().size2();</div>
<div class="line"><a id="l00017" name="l00017"></a><span class="lineno">   17</span>    std::size_t image_size1 = image().size1()/num_channels;</div>
<div class="line"><a id="l00018" name="l00018"></a><span class="lineno">   18</span>    std::size_t image_size2 = image().size2();</div>
<div class="line"><a id="l00019" name="l00019"></a><span class="lineno">   19</span>    std::size_t output_size1 = image_size1 - filter_size +1;</div>
<div class="line"><a id="l00020" name="l00020"></a><span class="lineno">   20</span>    std::size_t output_size2 = image_size2 - filter_size +1;</div>
<div class="line"><a id="l00021" name="l00021"></a><span class="lineno">   21</span>    <span class="keyword">typedef</span> <span class="keyword">typename</span> E1::value_type value_type;</div>
<div class="line"><a id="l00022" name="l00022"></a><span class="lineno">   22</span>    </div>
<div class="line"><a id="l00023" name="l00023"></a><span class="lineno">   23</span>    blas::matrix&lt;value_type&gt; out(output_size1 * num_filters, output_size2 ,0.0);</div>
<div class="line"><a id="l00024" name="l00024"></a><span class="lineno">   24</span>    <span class="keywordtype">double</span> minOptTime = std::numeric_limits&lt;double&gt;::max();</div>
<div class="line"><a id="l00025" name="l00025"></a><span class="lineno">   25</span>    <span class="keywordflow">for</span>(std::size_t i = 0; i != 20; ++i){</div>
<div class="line"><a id="l00026" name="l00026"></a><span class="lineno">   26</span>        <a class="code hl_class" href="classshark_1_1_timer.html" title="Timer abstraction with microsecond resolution.">Timer</a> time;</div>
<div class="line"><a id="l00027" name="l00027"></a><span class="lineno">   27</span>        blas::kernels::conv2d(image,filter,out, num_channels, num_filters);</div>
<div class="line"><a id="l00028" name="l00028"></a><span class="lineno">   28</span>        minOptTime = min(minOptTime,time.<a class="code hl_function" href="classshark_1_1_timer.html#ad3ccd47c0429d28d9600117b5ed57362" title="Returns the difference between current time and the start time.">stop</a>());</div>
<div class="line"><a id="l00029" name="l00029"></a><span class="lineno">   29</span>    }</div>
<div class="line"><a id="l00030" name="l00030"></a><span class="lineno">   30</span>    </div>
<div class="line"><a id="l00031" name="l00031"></a><span class="lineno">   31</span>    <span class="keywordtype">double</span> mults = output_size1 * output_size2 * filter_size * filter_size * num_filters * num_channels;</div>
<div class="line"><a id="l00032" name="l00032"></a><span class="lineno">   32</span>    <span class="keywordtype">double</span> flops = mults /1024/1024/minOptTime;</div>
<div class="line"><a id="l00033" name="l00033"></a><span class="lineno">   33</span>    </div>
<div class="line"><a id="l00034" name="l00034"></a><span class="lineno">   34</span>    std::cout&lt;&lt;output_size1&lt;&lt;<span class="stringliteral">&quot;\t&quot;</span>&lt;&lt;filter_size&lt;&lt;<span class="stringliteral">&quot;\t&quot;</span>&lt;&lt;num_channels&lt;&lt;<span class="stringliteral">&quot;\t&quot;</span>&lt;&lt; num_filters&lt;&lt;<span class="stringliteral">&quot;\t&quot;</span>;</div>
<div class="line"><a id="l00035" name="l00035"></a><span class="lineno">   35</span>    std::cout&lt;&lt;<span class="stringliteral">&quot;\t&quot;</span>&lt;&lt;flops&lt;&lt; std::endl;</div>
<div class="line"><a id="l00036" name="l00036"></a><span class="lineno">   36</span>}</div>
</div>
<div class="line"><a id="l00037" name="l00037"></a><span class="lineno">   37</span> </div>
<div class="line"><a id="l00038" name="l00038"></a><span class="lineno">   38</span> </div>
<div class="foldopen" id="foldopen00039" data-start="{" data-end="}">
<div class="line"><a id="l00039" name="l00039"></a><span class="lineno"><a class="line" href="conv2d_8cpp.html#a3c04138a5bfe5d72780bb7e82a18e627">   39</a></span><span class="keywordtype">int</span> <a class="code hl_function" href="_datasets_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a>(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv) {</div>
<div class="line"><a id="l00040" name="l00040"></a><span class="lineno">   40</span>    std::cout&lt;&lt;<span class="stringliteral">&quot;Flops&quot;</span>&lt;&lt;std::endl;</div>
<div class="line"><a id="l00041" name="l00041"></a><span class="lineno">   41</span>    std::size_t num_channels = 8;</div>
<div class="line"><a id="l00042" name="l00042"></a><span class="lineno">   42</span>    std::size_t num_outputs = 16;</div>
<div class="line"><a id="l00043" name="l00043"></a><span class="lineno">   43</span>    std::cout&lt;&lt;<span class="stringliteral">&quot;performance float&quot;</span>&lt;&lt;std::endl;</div>
<div class="line"><a id="l00044" name="l00044"></a><span class="lineno">   44</span>    <span class="keywordflow">for</span>(std::size_t filterSize = 4; filterSize != 32; filterSize *= 2){</div>
<div class="line"><a id="l00045" name="l00045"></a><span class="lineno">   45</span>        <span class="keywordflow">for</span>(std::size_t iter = 0; iter != 6; ++iter){</div>
<div class="line"><a id="l00046" name="l00046"></a><span class="lineno">   46</span>            std::size_t sizeOut1 = (3+16 * 2&lt;&lt;iter);</div>
<div class="line"><a id="l00047" name="l00047"></a><span class="lineno">   47</span>            std::size_t sizeOut2 = (3+16 * 2&lt;&lt;iter);</div>
<div class="line"><a id="l00048" name="l00048"></a><span class="lineno">   48</span>            std::size_t sizeIm1 = sizeOut1 + filterSize-1;</div>
<div class="line"><a id="l00049" name="l00049"></a><span class="lineno">   49</span>            std::size_t sizeIm2 = sizeOut2 + filterSize-1;</div>
<div class="line"><a id="l00050" name="l00050"></a><span class="lineno">   50</span> </div>
<div class="line"><a id="l00051" name="l00051"></a><span class="lineno">   51</span>            blas::matrix&lt;float&gt; image(num_channels * sizeIm1 , sizeIm2);</div>
<div class="line"><a id="l00052" name="l00052"></a><span class="lineno">   52</span>            blas::matrix&lt;float&gt; filter(num_channels * num_outputs *  filterSize, filterSize);</div>
<div class="line"><a id="l00053" name="l00053"></a><span class="lineno">   53</span>            </div>
<div class="line"><a id="l00054" name="l00054"></a><span class="lineno">   54</span>            <span class="keywordflow">for</span>(std::size_t i = 0; i != num_channels * sizeIm1; ++i){</div>
<div class="line"><a id="l00055" name="l00055"></a><span class="lineno">   55</span>                <span class="keywordflow">for</span>(std::size_t j = 0; j != sizeIm2; ++j){</div>
<div class="line"><a id="l00056" name="l00056"></a><span class="lineno">   56</span>                    image(i,j)  = 1.0/(num_channels * sizeOut1)*i + 0.1 - (0.1/sizeOut2)*j;</div>
<div class="line"><a id="l00057" name="l00057"></a><span class="lineno">   57</span>                }</div>
<div class="line"><a id="l00058" name="l00058"></a><span class="lineno">   58</span>            }</div>
<div class="line"><a id="l00059" name="l00059"></a><span class="lineno">   59</span>            <span class="keywordflow">for</span>(std::size_t i = 0; i != num_channels * num_outputs * filterSize; ++i){</div>
<div class="line"><a id="l00060" name="l00060"></a><span class="lineno">   60</span>                <span class="keywordflow">for</span>(std::size_t j = 0; j != filterSize; ++j){</div>
<div class="line"><a id="l00061" name="l00061"></a><span class="lineno">   61</span>                    filter(i,j)  = 1.0/(num_channels * filterSize)*i + 0.1 - (0.1/filterSize)*j;</div>
<div class="line"><a id="l00062" name="l00062"></a><span class="lineno">   62</span>                }</div>
<div class="line"><a id="l00063" name="l00063"></a><span class="lineno">   63</span>            }</div>
<div class="line"><a id="l00064" name="l00064"></a><span class="lineno">   64</span>            </div>
<div class="line"><a id="l00065" name="l00065"></a><span class="lineno">   65</span>            <a class="code hl_function" href="conv2d_8cpp.html#aa29c4b1ecd308e3486ba80c16f257484">benchmark</a>(image,filter,num_channels,num_outputs);           </div>
<div class="line"><a id="l00066" name="l00066"></a><span class="lineno">   66</span>        }</div>
<div class="line"><a id="l00067" name="l00067"></a><span class="lineno">   67</span>    }</div>
<div class="line"><a id="l00068" name="l00068"></a><span class="lineno">   68</span>    num_outputs = 8;</div>
<div class="line"><a id="l00069" name="l00069"></a><span class="lineno">   69</span>    std::cout&lt;&lt;<span class="stringliteral">&quot;performance double&quot;</span>&lt;&lt;std::endl;</div>
<div class="line"><a id="l00070" name="l00070"></a><span class="lineno">   70</span>    <span class="keywordflow">for</span>(std::size_t filterSize = 4; filterSize != 32; filterSize *= 2){</div>
<div class="line"><a id="l00071" name="l00071"></a><span class="lineno">   71</span>        <span class="keywordflow">for</span>(std::size_t iter = 0; iter != 6; ++iter){</div>
<div class="line"><a id="l00072" name="l00072"></a><span class="lineno">   72</span>            std::size_t sizeOut1 = (3+16 * 2&lt;&lt;iter);</div>
<div class="line"><a id="l00073" name="l00073"></a><span class="lineno">   73</span>            std::size_t sizeOut2 = (3+16 * 2&lt;&lt;iter);</div>
<div class="line"><a id="l00074" name="l00074"></a><span class="lineno">   74</span>            std::size_t sizeIm1 = sizeOut1 + filterSize-1;</div>
<div class="line"><a id="l00075" name="l00075"></a><span class="lineno">   75</span>            std::size_t sizeIm2 = sizeOut2 + filterSize-1;</div>
<div class="line"><a id="l00076" name="l00076"></a><span class="lineno">   76</span> </div>
<div class="line"><a id="l00077" name="l00077"></a><span class="lineno">   77</span>            blas::matrix&lt;double&gt; image(num_channels * sizeIm1 , sizeIm2);</div>
<div class="line"><a id="l00078" name="l00078"></a><span class="lineno">   78</span>            blas::matrix&lt;double&gt; filter(num_channels * num_outputs *  filterSize, filterSize);</div>
<div class="line"><a id="l00079" name="l00079"></a><span class="lineno">   79</span>            </div>
<div class="line"><a id="l00080" name="l00080"></a><span class="lineno">   80</span>            <span class="keywordflow">for</span>(std::size_t i = 0; i != num_channels * sizeIm1; ++i){</div>
<div class="line"><a id="l00081" name="l00081"></a><span class="lineno">   81</span>                <span class="keywordflow">for</span>(std::size_t j = 0; j != sizeIm2; ++j){</div>
<div class="line"><a id="l00082" name="l00082"></a><span class="lineno">   82</span>                    image(i,j)  = 1.0/(num_channels * sizeOut1)*i + 0.1 - (0.1/sizeOut2)*j;</div>
<div class="line"><a id="l00083" name="l00083"></a><span class="lineno">   83</span>                }</div>
<div class="line"><a id="l00084" name="l00084"></a><span class="lineno">   84</span>            }</div>
<div class="line"><a id="l00085" name="l00085"></a><span class="lineno">   85</span>            <span class="keywordflow">for</span>(std::size_t i = 0; i != num_channels * num_outputs * filterSize; ++i){</div>
<div class="line"><a id="l00086" name="l00086"></a><span class="lineno">   86</span>                <span class="keywordflow">for</span>(std::size_t j = 0; j != filterSize; ++j){</div>
<div class="line"><a id="l00087" name="l00087"></a><span class="lineno">   87</span>                    filter(i,j)  = 1.0/(num_channels * filterSize)*i + 0.1 - (0.1/filterSize)*j;</div>
<div class="line"><a id="l00088" name="l00088"></a><span class="lineno">   88</span>                }</div>
<div class="line"><a id="l00089" name="l00089"></a><span class="lineno">   89</span>            }</div>
<div class="line"><a id="l00090" name="l00090"></a><span class="lineno">   90</span>            </div>
<div class="line"><a id="l00091" name="l00091"></a><span class="lineno">   91</span>            <a class="code hl_function" href="conv2d_8cpp.html#aa29c4b1ecd308e3486ba80c16f257484">benchmark</a>(image,filter,num_channels,num_outputs);           </div>
<div class="line"><a id="l00092" name="l00092"></a><span class="lineno">   92</span>        }</div>
<div class="line"><a id="l00093" name="l00093"></a><span class="lineno">   93</span>    }</div>
<div class="line"><a id="l00094" name="l00094"></a><span class="lineno">   94</span>}</div>
</div>
</div><!-- fragment --></div><!-- contents -->
</div>
</body>
</html>
